var Plan=require('../models/planModel.js');
var code=require('../txtCode/code.js');
module.exports={
	GetListHtml:function(req,res,next){
		if(req.session){
			res.render('planList',function(err,html){
				if(err) console.error(err);
				res.send(html);
			});//res.render
		}//if(req.session)
	},//GetListHtml
	GetAddHtml:function(req,res,next){
		if(req.session){
			res.render('planAdd',function(err,html){
				if(err) console.error(err);
				res.send(html);
			});//res.render
		}//if(req.session)
	},//GetAddHtml
	SavePlan:function(req,res,next){
		if(req.session){
			var data=req.body;
			var oAddPlan=new Plan(data);
			oAddPlan.save().then(
				function(data){
					res.send({code:code.success});
				},
				function(err){
					console.log(err);
					res.send({code:code.insertFail});
				});//oAddPlan
		}//if(req.session)
	},//SavePlan()
	GetPageQuantity:function(req,res,next){
		if(req.session){
		Plan
		.find()
		.sort()
		.count(function(err,count){
			if(err) console.log(err);
			console.log(count);
			var pageQuantity=Math.ceil(count/10);
			res.send({
				code:code.success,
				data:{'pageQuantity':pageQuantity}});
			});
		}//if(req.session)
	},//GetPageQuantity
	
	FirstShow:function(req,res,next){
		if(req.session){
			Plan
			.find()
			.sort()
			.limit(10)
			.then(
				function(result){
					res.send({code:code.success,data:result});
				},
				function(err){
					if(err) console.log(err);
					res.send({code:code.findFail});
				})//Plan
		}//if(req.session)
	},//FirstShow
	ShowPage : function(req,res,next){
		if(req.session){
			var PAGE_SIZE=10;
			var pageNumber=(parseInt(req.body.pageNumber)-1)*PAGE_SIZE;
			Plan
			.find()
			.sort()
			.skip(pageNumber)
			.limit(10)
			.then(function(result){
				console.log(result);
				res.send({
					code:[1000,'success'],
					data:result
				});
			});
		}//if(req.session)
	},//ShowPage
	FetchPlan:function(req,res,next){
		if(req.session){
			console.log(req.body);
			var planname=req.body.needPlanname;
			Plan
			.find({planname:planname})
			.then(function(result){
				res.send({
					code:[1000,'success'],
					data:result
				});
			})
		}//if(req.session)
	},//FetchMotion
	ConditionPlan:function(req,res,next){
		if(req.session){
			console.log(req.body);
			var condition=req.body;
			Plan
			.find(condition)
			.then(function(result){
				res.send({
					code:[1000,'success'],
					data:result
				});
			});//then
		};//if(req.session)
	},//ConditionPlan
	DeletePlan:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			Plan
			.remove(data,function(err,result){
				if(err) console.log(err);
				// console.log(result);
				res.send({
					code:[1000,'success'],
					data:'删除成功'
				});
			})

		};//if(req.session)
	},//DeletePlan
}